package com.vasthu.test;

import com.vasthu.api.domain.Role;
import com.vasthu.api.domain.User;
import com.vasthu.api.domain.Item;
import com.vasthu.api.domain.Category;
import com.vasthu.api.domain.Stock;
import com.vasthu.gui.VstPluginManager;
import com.vasthu.api.service.RoleManagerService;
import com.vasthu.api.service.UserManagerService;
import com.vasthu.api.service.CategoryManagerService;
import com.vasthu.api.service.StockManagerService;
import com.vasthu.api.service.ItemManagerService;
import com.vasthu.core.service.UserManagerServiceImpl;
import com.vasthu.core.service.RoleManagerServiceImpl;
import com.vasthu.core.service.CategoryManagerServiceImpl;
import com.vasthu.core.service.StockManagerServiceImpl;
import com.vasthu.core.service.ItemManagerServiceImpl;
import com.vasthu.gui.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;

public class VstMainWindow extends JFrame
{
	private JPanel mainPanel;
	int i;
	
	private UserManagerService 	userManagerService 		= null;
	private RoleManagerService 	roleManagerService 		= null;
	private CategoryManagerService 	categoryManagerService 	= null;
	private ItemManagerService 	itemManagerService 	= null;
	private StockManagerService 	stockManagerService 	= null;
	
	/* Constructor */
	public VstMainWindow()
	{
		i = 0;
		initUi();
		loadPlugins();
		
		userManagerService 		= new UserManagerServiceImpl();
		roleManagerService 		= new RoleManagerServiceImpl();
		categoryManagerService 	= new CategoryManagerServiceImpl();
		itemManagerService 		= new ItemManagerServiceImpl();
		stockManagerService 		= new StockManagerServiceImpl();
	}
	
	/* Initializes GUI widgets */
	private void initUi()
	{
		ImageIcon exitIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "exit.png"));
		ImageIcon exitIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "exit24.png"));
		ImageIcon addUserIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_user.png"));
		ImageIcon addUserIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_user24.png"));
		ImageIcon editUserIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_user.png"));
		ImageIcon editUserIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_user24.png"));
		ImageIcon addItemIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_item.png"));
		ImageIcon addItemIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_item24.png"));
		ImageIcon editItemIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_item.png"));
		ImageIcon editItemIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_item24.png"));		
		ImageIcon addCatIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_category.png"));
		ImageIcon addCatIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_category24.png"));
		ImageIcon editCatIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_category.png"));
		ImageIcon editCatIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_category24.png"));
		ImageIcon addBillIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_bill.png"));
		ImageIcon addBillIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_bill24.png"));
		ImageIcon editBillIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_bill.png"));
		ImageIcon editBillIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_bill24.png"));
		ImageIcon addStockIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_store.png"));
		ImageIcon addStockIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_store24.png"));
		ImageIcon editStockIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_store.png"));
		ImageIcon editStockIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_store24.png"));	
		ImageIcon addRoleIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_role.png"));
		ImageIcon addRoleIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "add_role24.png"));
		ImageIcon editRoleIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_role.png"));
		ImageIcon editRoleIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "edit_role24.png"));
		ImageIcon reportIcon = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "report.png"));
		ImageIcon reportIcon24 = new ImageIcon(getClass().getResource(VstGuiConstants.IMAGE_PATH + "report24.png"));
		
		/* Adding menu bar */
		JMenuBar menuBar = new JMenuBar();
		JMenu sys = new JMenu("System");
		JMenuItem actionAddUser = new JMenuItem("Add User", addUserIcon24);
		actionAddUser.setToolTipText("Add New User");
		JMenuItem actionEditUser = new JMenuItem("Edit User", editUserIcon24);
		actionEditUser.setToolTipText("Edit User");
		JMenuItem actionAddItem = new JMenuItem("Add Item", addItemIcon24);
		actionAddItem.setToolTipText("Add New Item");
		JMenuItem actionEditItem = new JMenuItem("Edit Item", editItemIcon24);
		actionEditItem.setToolTipText("Edit Item");	
		JMenuItem actionAddCat = new JMenuItem("Add Category", addCatIcon24);
		actionAddCat.setToolTipText("Add New Category");
		JMenuItem actionEditCat = new JMenuItem("Edit Category", editCatIcon24);
		actionEditCat.setToolTipText("Edit Category");
		JMenuItem actionAddBill = new JMenuItem("Add Bill", addBillIcon24);
		actionAddBill.setToolTipText("Add New Bill");
		JMenuItem actionEditBill = new JMenuItem("Edit Bill", editBillIcon24);
		actionEditBill.setToolTipText("Edit Bill");	
		JMenuItem actionAddStock = new JMenuItem("Add Stock Item", addStockIcon24);
		actionAddStock.setToolTipText("Add New Stock Item");
		JMenuItem actionEditStock = new JMenuItem("Edit Stock Item", editStockIcon24);
		actionEditStock.setToolTipText("Edit Stock Item");
		JMenuItem actionAddRole = new JMenuItem("Add Role", addRoleIcon24);
		actionAddRole.setToolTipText("Add New Role");
		JMenuItem actionEditRole = new JMenuItem("Edit Role", editRoleIcon24);
		actionEditRole.setToolTipText("Edit Role");	
		JMenuItem actionReport = new JMenuItem("Generate Report", reportIcon24);
		actionReport.setToolTipText("Generate Report");	
		
		JMenuItem actionExit = new JMenuItem("Exit", exitIcon24);
		actionExit.setToolTipText("Exit application");
		
		/* Add User menu item clicked */
		actionAddUser.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addNewUserClicked();
            }
        });
		
		/* Edit User menu item clicked */
		actionEditUser.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
				try {
					updateUserClicked();
				} catch (Exception ex) {}
            }
        });
		
		/* Add Item menu item clicked */
		actionAddItem.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addNewItemClicked();
            }
        });
		
		/* Edit Item menu item clicked */
		actionEditItem.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateItemClicked();
            }
        });
		
		/* Add Category menu item clicked */
		actionAddCat.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addCategoryClicked();
            }
        });
		
		/* Edit Category menu item clicked */
		actionEditCat.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateCategoryClicked();
            }
        });
		
		/* Add Bill menu item clicked */
		actionAddBill.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addBillClicked();
            }
        });
		
		/* Edit Bill menu item clicked */
		actionEditBill.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateBillClicked();
            }
        });
		
		/* Add Stock menu item clicked */
		actionAddStock.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addStockItemClicked();
            }
        });
		
		/* Edit Stock menu item clicked */
		actionEditStock.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateStockItemClicked();
            }
        });
		
		/* Add Role menu item clicked */
		actionAddRole.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addRoleClicked();
            }
        });
		
		/* Edit Role menu item clicked */
		actionEditRole.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateRoleClicked();
            }
        });
		
		/* Generate Report menu item clicked */
		actionReport.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                generateReportClicked();
            }
        });
		
		/* Exit menu item clicked */
		actionExit.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                System.exit(0);
            }
        });
		
		sys.add(actionAddUser);
		sys.add(actionEditUser);
		sys.add(actionAddRole);
		sys.add(actionEditRole);
		sys.add(actionAddBill);
		sys.add(actionEditBill);
		sys.add(actionAddItem);
		sys.add(actionEditItem);
		sys.add(actionAddCat);
		sys.add(actionEditCat);
		sys.add(actionAddStock);
		sys.add(actionEditStock);
		sys.add(actionReport);
		sys.add(actionExit);
		menuBar.add(sys);
		
		setJMenuBar(menuBar);
		VstPluginManager.get().setMenuBar(menuBar);
		
		/* Adding Tool bar */
		JToolBar toolBar = new JToolBar();
		JButton exitButton = new JButton(exitIcon);
		exitButton.setToolTipText("Exit application");
		JButton addButton = new JButton(addUserIcon);
		addButton.setToolTipText("Add New User");
		JButton editButton = new JButton(editUserIcon);
		editButton.setToolTipText("Edit User");
		JButton addItemButton = new JButton(addItemIcon);
		addItemButton.setToolTipText("Add Item");
		JButton editItemButton = new JButton(editItemIcon);
		editItemButton.setToolTipText("Edit Item");
		JButton addCatButton = new JButton(addCatIcon);
		addCatButton.setToolTipText("Add Category");
		JButton editCatButton = new JButton(editCatIcon);
		editCatButton.setToolTipText("Edit Category");
		JButton addBillButton = new JButton(addBillIcon);
		addBillButton.setToolTipText("Add Bill");
		JButton editBillButton = new JButton(editBillIcon);
		editBillButton.setToolTipText("Edit Bill");	
		JButton addStockButton = new JButton(addStockIcon);
		addStockButton.setToolTipText("Add Stock Item");
		JButton editStockButton = new JButton(editStockIcon);
		editStockButton.setToolTipText("Edit Stock Item");
		JButton addRoleButton = new JButton(addRoleIcon);
		addRoleButton.setToolTipText("Add Role");
		JButton editRoleButton = new JButton(editRoleIcon);
		editRoleButton.setToolTipText("Edit Role");
		JButton reportButton = new JButton(reportIcon);
		reportButton.setToolTipText("Generate Report");
		
		/* Exit tool button item clicked */
		exitButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                System.exit(0);
            }
        });
		
		/* Add User tool button item clicked */
		addButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addNewUserClicked();
            }
        });

		/* Edit User tool button item clicked */
		editButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                try {
					updateUserClicked();
				} catch (Exception ex) {}
            }
        });

		/* Add Item tool button item clicked */
		addItemButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addNewItemClicked();
            }
        });
		
		/* Edit Item tool button item clicked */
		editItemButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateItemClicked();
            }
        });
		
		/* Add Category tool button item clicked */
		addCatButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addCategoryClicked();
            }
        });
		
		/* Edit Category tool button item clicked */
		editCatButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateCategoryClicked();
            }
        });
		
		/* Add Bill tool button item clicked */
		addBillButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addBillClicked();
            }
        });
		
		/* Edit Bill tool button item clicked */
		editBillButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateBillClicked();
            }
        });
		
		/* Add Stock tool button item clicked */
		addStockButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addStockItemClicked();
            }
        });
		
		/* Edit Stock tool button item clicked */
		editStockButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateStockItemClicked();
            }
        });
		
		/* Add Role tool button item clicked */
		addRoleButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                addRoleClicked();
            }
        });
		
		/* Edit Role tool button item clicked */
		editRoleButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                updateRoleClicked();
            }
        });
		
		/* Generate Report tool button item clicked */
		reportButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                generateReportClicked();
            }
        });
		
		toolBar.add(exitButton);
		toolBar.add(addButton);
		toolBar.add(editButton);
		toolBar.add(addRoleButton);
		toolBar.add(editRoleButton);
		toolBar.add(addBillButton);
		toolBar.add(editBillButton);
		toolBar.add(addItemButton);
		toolBar.add(editItemButton);
		toolBar.add(addCatButton);
		toolBar.add(editCatButton);
		toolBar.add(addStockButton);
		toolBar.add(editStockButton);
		toolBar.add(reportButton);
		
		add(toolBar, BorderLayout.NORTH);
		
		/* Adding Card layout */
		mainPanel = new JPanel(new CardLayout());
		
		JPanel p1 = new JPanel();
		p1.setBackground(new Color(238,233,233));
		JLabel l1 = new JLabel("P1");
		p1.add(l1);
		JPanel p2 = new JPanel();
		p2.setBackground(new Color(205,201,201));
		JLabel l2 = new JLabel("P2");
		p2.add(l2);
		
		mainPanel.add(p1, "p1");
		mainPanel.add(p2, "p2");
		add(mainPanel, BorderLayout.CENTER);
		
		JButton b = new JButton("Click");
		b.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent event) 
			{
                CardLayout cl = (CardLayout)(mainPanel.getLayout());
				if (i == 0) 
				{
					cl.show(mainPanel, "p2");
					i = 1;
				}
				else
				{
					cl.show(mainPanel, "p1");
					i = 0;
				}
            }
        });
		
		add(b, BorderLayout.SOUTH);
		
		setMinimumSize(new Dimension(300, 300));
		setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void addNewUserClicked() {
		VstUserDataForm udForm = new VstUserDataForm(VstGuiConstants.USER_FORM_ADD, -1, null);
		udForm.setVisible(true);
	}
	
	public void updateUserClicked() throws Exception {
		VstSelectionTableWindow userSelectionTable = new VstSelectionTableWindow() {
			protected VstTableItemModel installTableModel() {
				List<VstTableItemSelectable> dataList = userManagerService.getAllUsersForTable();
				return new VstTableItemModel(dataList, VstGuiConstants.USER_COL_NAMES, VstGuiConstants.USER_COL_CLASSES);
			}
	
			protected void okButtonClicked() {
				VstTableItemModel tModel = (VstTableItemModel)getModel();
				if (getSelectedRow() == -1)
					return;
					
				VstTableItemSelectable object = tModel.getObjecAtRow(getSelectedRow());
				
				if (object != null) {
					User user = (User) object;
					VstUserDataForm udForm = new VstUserDataForm(VstGuiConstants.USER_FORM_UPDATE, getSelectedRow(), this);
					udForm.populate(user);
					udForm.setVisible(true);
				}
			}
		};
		userSelectionTable.setVisible(true);
	}
	
	public void addNewItemClicked() {
		VstItemDataForm iForm = new VstItemDataForm(VstGuiConstants.ITEM_FORM_ADD, -1, null);
		iForm.setVisible(true);
	}
	
	public void updateItemClicked() {
		VstSelectionTableWindow itemSelectionTable = new VstSelectionTableWindow() {
			protected VstTableItemModel installTableModel() {
				List<VstTableItemSelectable> dataList = itemManagerService.getAllItemsForTable();
				return new VstTableItemModel(dataList, VstGuiConstants.ITEM_COL_NAMES, VstGuiConstants.ITEM_COL_CLASSES);
			}
	
			protected void okButtonClicked() {
				VstTableItemModel tModel = (VstTableItemModel)getModel();
				if (getSelectedRow() == -1)
					return;
					
				VstTableItemSelectable object = tModel.getObjecAtRow(getSelectedRow());
				
				if (object != null) {
					Item item = (Item) object;
					VstItemDataForm iForm = new VstItemDataForm(VstGuiConstants.ITEM_FORM_UPDATE, getSelectedRow(), this);
					iForm.populate(item);
					iForm.setVisible(true);
				}
			}
		};
		itemSelectionTable.setVisible(true);
	}
	
	public void addRoleClicked() {
		VstRoleDataForm udForm = new VstRoleDataForm(VstGuiConstants.ROLE_FORM_ADD, -1, null);
		udForm.setVisible(true);
	}
	
	public void updateRoleClicked() {
		VstSelectionTableWindow roleSelectionTable = new VstSelectionTableWindow() {
			protected VstTableItemModel installTableModel() {
				List<VstTableItemSelectable> dataList = roleManagerService.getAllRolesForTable();
				return new VstTableItemModel(dataList, VstGuiConstants.ROLE_COL_NAMES, VstGuiConstants.ROLE_COL_CLASSES);
			}
	
			protected void okButtonClicked() {
				VstTableItemModel tModel = (VstTableItemModel)getModel();
				if (getSelectedRow() == -1)
					return;
					
				VstTableItemSelectable object = tModel.getObjecAtRow(getSelectedRow());
				
				if (object != null) {
					Role role = (Role) object;
					VstRoleDataForm rForm = new VstRoleDataForm(VstGuiConstants.ROLE_FORM_UPDATE, getSelectedRow(), this);
					rForm.populate(role);
					rForm.setVisible(true);
				}
			}
		};
		roleSelectionTable.setVisible(true);
	}
	
	public void addBillClicked() {
	}
	
	public void updateBillClicked() {
		//VstSelectionTableWindow billSelectionTable = new VstSelectionTableWindow();
		//billSelectionTable.setVisible(true);
	}
	
	public void addCategoryClicked() {
		VstCategoryDataForm udForm = new VstCategoryDataForm(VstGuiConstants.CATEGORY_FORM_ADD, -1, null);
		udForm.setVisible(true);
	}
	
	public void updateCategoryClicked() {
		VstSelectionTableWindow categorySelectionTable = new VstSelectionTableWindow() {
			protected VstTableItemModel installTableModel() {
				List<VstTableItemSelectable> dataList = categoryManagerService.getAllCategorysForTable();
				return new VstTableItemModel(dataList, VstGuiConstants.CAT_COL_NAMES, VstGuiConstants.CAT_COL_CLASSES);
			}
	
			protected void okButtonClicked() {
				VstTableItemModel tModel = (VstTableItemModel)getModel();
				if (getSelectedRow() == -1)
					return;
					
				VstTableItemSelectable object = tModel.getObjecAtRow(getSelectedRow());
				
				if (object != null) {
					Category cat = (Category) object;
					VstCategoryDataForm cForm = new VstCategoryDataForm(VstGuiConstants.CATEGORY_FORM_UPDATE, getSelectedRow(), this);
					cForm.populate(cat);
					cForm.setVisible(true);
				}
			}
		};
		categorySelectionTable.setVisible(true);
	}
	
	public void addStockItemClicked() {
		VstStockDataForm sForm = new VstStockDataForm(VstGuiConstants.STOCK_FORM_ADD, -1, null);
		sForm.setVisible(true);
	}
	
	public void updateStockItemClicked() {
		VstSelectionTableWindow stockSelectionTable = new VstSelectionTableWindow() {
			protected VstTableItemModel installTableModel() {
				List<VstTableItemSelectable> dataList = stockManagerService.getAllStockedItemsForTable();
				return new VstTableItemModel(dataList, VstGuiConstants.STOCK_COL_NAMES, VstGuiConstants.STOCK_COL_CLASSES);
			}
	
			protected void okButtonClicked() {
				VstTableItemModel tModel = (VstTableItemModel)getModel();
				if (getSelectedRow() == -1)
					return;
					
				VstTableItemSelectable object = tModel.getObjecAtRow(getSelectedRow());
				
				if (object != null) {
					Stock stock = (Stock) object;
					VstStockDataForm cForm = new VstStockDataForm(VstGuiConstants.STOCK_FORM_UPDATE, getSelectedRow(), this);
					cForm.populate(stock);
					cForm.setVisible(true);
				}
			}
		};
		stockSelectionTable.setVisible(true);
	}
	
	public void generateReportClicked() {
	}
	
	public void loadPlugins() {
		VstPluginManager.get().processXml();
	}
	
	public static void main(String[] args)
	{
		/*try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (Exception ex) {
            System.out.println(ex);
        }*/
		//JFrame.setDefaultLookAndFeelDecorated(true);
		//JDialog.setDefaultLookAndFeelDecorated(true);
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				VstMainWindow mainWindow = new VstMainWindow();
				mainWindow.setVisible(true);
			}
		});
	}
}